package com.me.darkness_is_coming.actors;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

import com.me.darkness_is_coming.screens.GameScreen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;


public class Darkness extends Actor
{
    private float Penalti;
    //private Texture texture;
    private TextureAtlas darknessImages;
    private TextureRegion darkness;
    public boolean darkness_coming_animation;
    private int darkness_state;
    private float darkness_update_time;
    private float darkness_timer;
    //private boolean is_game_over;

    public Darkness()
    {
        darkness_state = 1;
        darkness_coming_animation = true;
        darkness_update_time = 1.1f;
        darkness_timer = 0.0f;
        darknessImages = new TextureAtlas(Gdx.files.internal("data/Darkness/Darkness.atlas"));
        darkness = new TextureRegion(darknessImages.findRegion("Darkness01"));
        Penalti = 0f;
        setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() * 1.1f);
        setPosition(0, -4 * Gdx.graphics.getHeight());
    }

    @Override
    public void act(float delta){
        //if (!is_game_over)
        //{
            if (Penalti <= 0f) {
                GameScreen.DarknessSpeed += GameScreen.DarknessAcceleration * delta;
                setY(getY() + (GameScreen.DarknessSpeed - GameScreen.CloudMushroomSpeed) * delta);
                //System.out.println(">>>Penalti <= 0f");
            } else {
                //System.out.println(">>>Penalti > 0f");
                setY(getY() + (-GameScreen.CloudMushroomSpeed) * delta);
                Penalti -= delta;
            }
        //}
    }

    public void addPenalti(float penalti){
        Penalti += penalti;
    }

    @Override
    public void draw(Batch batch, float parentAlpha)
    {
        batch.setColor(getColor());
        batch.draw(darkness, getX(), getY(), getWidth(), getHeight());
    }

    private void MoveDarkness()
    {
        if (darkness_coming_animation)
        {
            if (getY() < -Gdx.graphics.getHeight()*0.01f)
            {
                System.out.println(">>>Move Darkness");
                setY(getY() + Gdx.graphics.getHeight() * 0.01f);
            }
            else darkness_coming_animation = false;
        }
    }

    private void UpdateDarknessImage()
    {
        darkness = darknessImages.findRegion("Darkness0" + darkness_state);
        if (darkness_state == 3) darkness_state = 1;
        else darkness_state++;
    }

    public void UpdateDarkness(boolean is_game_over, float delta)
    {
        if (is_game_over)
        {
            MoveDarkness();
        }
        if (darkness_timer + delta > darkness_update_time)
        {
            UpdateDarknessImage();
            darkness_timer = 0;
        }
        else darkness_timer += delta;
    }
    public void clear(){
        darkness_state = 1;
        darkness_coming_animation = true;
        darkness_update_time = 1.1f;
        darkness_timer = 0.0f;
        Penalti = 0f;
        setPosition(0, -4 * Gdx.graphics.getHeight());
    }

    public void dispose()
    {
        darknessImages.dispose();
    }
}
